package sys.controller;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
import sys.core.ResponseResult;
import sys.core.annotation.RecordLog;
import sys.core.exception.BusinessException;
import sys.entity.TbUserEntity;
import sys.entity.User;
import sys.entity.VerifyCode;
import sys.service.IVerifyCodeGen;
import sys.service.TbUserService;
import sys.service.impl.SimpleCharVerifyCodeGenImpl;
import sys.utils.MD5Utils;
import sys.utils.SessionUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Random;

/**
 * @author Administrator
 */
@Slf4j
@RestController
@Api(tags = "登录接口")
@RequestMapping("/login")
public class LoginController {
    @Autowired
    private TbUserService userService;

    @Autowired
    private RedisTemplate redisTemplate;

    @ApiOperation(value = "登录")
    @PostMapping(value = "/doLogin")
    @RecordLog(value = "登录")
    public ResponseResult doLogin(@RequestBody User loginUser, HttpServletRequest request) {
        log.info("验证码前台{}",loginUser.getCode());
        //转为大写
        loginUser.setCode(loginUser.getCode().toUpperCase());

        log.info("验证码前台转后{}",loginUser.getCode());
        log.info("用户,{}",loginUser);
        if (loginUser.getCode().equals("") || loginUser.getCode().length() == 0) {
            throw new BusinessException("验证码不能为空");
        }
        String code = (String) redisTemplate.opsForValue().get("verifyCode");
        log.info("验证码：{}",code);
        if (!loginUser.getCode().equals(code)) {
            throw new BusinessException("验证码有误");
        }
        return userService.login(loginUser, request);
    }


    @ApiOperation(value = "重置密码")
    @PostMapping(value = "/Reset")
    @RecordLog(value = "重置密码")
    public ResponseResult Reset(Integer id) {
        TbUserEntity userEntity = new TbUserEntity();
        Random random = new Random();
        int i = random.nextInt(10000);
        String password = MD5Utils.encode("123456" +i);
        userEntity.setSalt(i);
        userEntity.setPassword(password);
        userEntity.setId(id);
        userService.update(userEntity);
        return ResponseResult.success(password);
    }
    @ApiOperation(value = "退出登录")
    @PostMapping(value = "/doLogOut")
    @RecordLog(value = "退出登录")
    public void doLogOut(HttpServletRequest request) {
        SessionUtil.logOut(request);
    }


    @ApiOperation(value = "验证码")
    @GetMapping("/verifyCode")
    public void verifyCode(HttpServletRequest request, HttpServletResponse response) {
        IVerifyCodeGen iVerifyCodeGen = new SimpleCharVerifyCodeGenImpl();
        try {
            //设置长宽
            VerifyCode verifyCode = iVerifyCodeGen.generate(80, 28);
            String code = verifyCode.getCode();
            log.info("{}", code);

            //存入redis
            redisTemplate.opsForValue().set("verifyCode", code);
            //将VerifyCode绑定session
            request.getSession().setAttribute("VerifyCode", code);
            //设置响应头
            response.setHeader("Pragma", "no-cache");
            //设置响应头
            response.setHeader("Cache-Control", "no-cache");
            //在代理服务器端防止缓冲
            response.setDateHeader("Expires", 0);
            //设置响应内容类型
            response.setContentType("image/jpeg");
            response.getOutputStream().write(verifyCode.getImgBytes());
            response.getOutputStream().flush();
        } catch (IOException e) {
            log.info("{}", e);
        }
    }
}
